#include <47func.h>
typedef struct shareRes_s {
    int turn;
    int wantsToEnter[2];
    int total;
}shareRes_t;
void *threadFunc(void *arg){
    shareRes_t *pShareRes = (shareRes_t *)arg;
    for(int i = 0; i < 10000000; ++i){
        pShareRes->wantsToEnter[0] = 1;
        while(pShareRes->wantsToEnter[1]){
            if(pShareRes->turn != 0){
                pShareRes->wantsToEnter[0] = 0;
                while(pShareRes->turn != 0){

                }
            }
            pShareRes->wantsToEnter[0] = 1;
        }
        ++pShareRes->total;

        pShareRes->turn = 1;
        pShareRes->wantsToEnter[0] = 0;
    }
    return NULL;
}
int main()
{
    shareRes_t shareRes;
    shareRes.total = 0;
    shareRes.turn = 0;
    shareRes.wantsToEnter[0] = 0;
    shareRes.wantsToEnter[1] = 0;
    pthread_t tid;
    pthread_create(&tid,NULL,threadFunc,&shareRes);
    for(int i = 0; i < 10000000; ++i){
        shareRes.wantsToEnter[1] = 1;
        while(shareRes.wantsToEnter[0]){
            if(shareRes.turn != 1){
                shareRes.wantsToEnter[1] = 0;
                while(shareRes.turn != 1){

                }
                shareRes.wantsToEnter[1] = 1;
            }
        }
        ++shareRes.total;
        shareRes.turn = 0;
        shareRes.wantsToEnter[1] = 0;
    }
    pthread_join(tid,NULL);
    printf("total = %d\n", shareRes.total);
    return 0;
}

